#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_RtpProtocol/RTPProtocol_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_RtpProtocol/RTPProtocol_Error.h"

//g++ -std=gnu++11 -Wall -g Test_RtpProtocol.cpp -o Test_RtpProtocol -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_StreamMedia -lNetEngine_BaseLib -lStreamMedia_RtpProtocol -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_StreamMedia,--disable-new-dtags

void TestPacket_RTP264()
{
    XNETHANDLE xhSsrc = 0;
    if (!RTPProtocol_Packet_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H264, FALSE))
    {
        printf("errrno");
        return;
    }
    RTPProtocol_Packet_SetInfo(xhSsrc);

    LPCTSTR lpsz264File = _T("1080P.264");
    LPCTSTR lpszRTPFile = _T("1080P.RTP");

    FILE *pSt_264File = _tfopen(lpsz264File, _T("rb"));
    FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("wb"));
    TCHAR tszMsgBuffer[10240];
    int i = 0;

    while (1)
    {
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_264File);
        if (nRet <= 0)
        {
            break;
        }
        RTPProtocol_Packet_Send(xhSsrc, tszMsgBuffer, nRet);
        while (1)
        {
            list<STREAMMEDIA_RTPPROTOCOL_PACKET> stl_RTPPacket;
            if (!RTPProtocol_Packet_Recv(xhSsrc, &stl_RTPPacket))
            {
                break;
            }
            list<STREAMMEDIA_RTPPROTOCOL_PACKET>::const_iterator stl_ListIterator = stl_RTPPacket.begin();
            for (; stl_ListIterator != stl_RTPPacket.end(); stl_ListIterator++)
            {
                printf("%d=%d\n", i, stl_ListIterator->nMsgLen);
                fwrite(stl_ListIterator->tszMsgBuffer, 1, stl_ListIterator->nMsgLen, pSt_RTPFile);
                i++;
            }
            stl_RTPPacket.clear();
        }
    }
    fclose(pSt_264File);
    fclose(pSt_RTPFile);
    RTPProtocol_Packet_Destory(xhSsrc);
}
void TestParse_RTP264()
{
    XNETHANDLE xhSsrc = 0;
    if (!RTPProtocol_Parse_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H264, FALSE))
    {
        printf("errrno");
        return;
    }
    LPCTSTR lpsz264File = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\1080_1.264");
    LPCTSTR lpszRTPFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\1080P.RTP");

    FILE* pSt_264File = _tfopen(lpsz264File, _T("wb"));
    FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("rb"));
    TCHAR tszMsgBuffer[10240];
    int i = 0;

    while (1)
    {
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_RTPFile);
        if (nRet <= 0)
        {
            break;
        }
        RTPProtocol_Parse_Send(xhSsrc, tszMsgBuffer, nRet);
        while (1)
        {
            int nMsgLen = 1024000;
            TCHAR* ptszMsgBuffer = new TCHAR[nMsgLen];
            STREAMMEDIA_RTPPROTOCOL_HDR st_RTPHdr;

            memset(ptszMsgBuffer, '\0', nMsgLen);
            memset(&st_RTPHdr, '\0', sizeof(STREAMMEDIA_RTPPROTOCOL_HDR));

            if (!RTPProtocol_Parse_Recv(xhSsrc, ptszMsgBuffer, &nMsgLen, &st_RTPHdr, NULL, NULL))
            {
                delete[] ptszMsgBuffer;
                ptszMsgBuffer = NULL;
                break;
            }
            printf("%d\n", i++);
            fwrite(ptszMsgBuffer, 1, nMsgLen, pSt_264File);
            delete[] ptszMsgBuffer;
            ptszMsgBuffer = NULL;
        }
    }
    fclose(pSt_264File);
    fclose(pSt_RTPFile);
    RTPProtocol_Parse_Destory(xhSsrc);
}
void TestPacket_RTP265()
{
    XNETHANDLE xhSsrc = 0;
    if (!RTPProtocol_Packet_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H265,FALSE))
    {
        printf("errrno");
        return;
    }

    RTPProtocol_Packet_SetInfo(xhSsrc);
    LPCTSTR lpsz264File = _T("2.hevc");
    LPCTSTR lpszRTPFile = _T("hevc.RTP");

    FILE* pSt_264File = _tfopen(lpsz264File, _T("rb"));
    FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("wb"));
    TCHAR tszMsgBuffer[10240];
    int i = 0;

    while (1)
    {
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_264File);
        if (nRet <= 0)
        {
            break;
        }
        RTPProtocol_Packet_Send(xhSsrc, tszMsgBuffer, nRet);
        while (1)
        {
            list<STREAMMEDIA_RTPPROTOCOL_PACKET> stl_RTPPacket;
            if (!RTPProtocol_Packet_Recv(xhSsrc, &stl_RTPPacket))
            {
                break;
            }
            list<STREAMMEDIA_RTPPROTOCOL_PACKET>::const_iterator stl_ListIterator = stl_RTPPacket.begin();
            for (; stl_ListIterator != stl_RTPPacket.end(); stl_ListIterator++)
            {
                printf("%d=%d\n", i, stl_ListIterator->nMsgLen);
                fwrite(stl_ListIterator->tszMsgBuffer, 1, stl_ListIterator->nMsgLen, pSt_RTPFile);
                i++;
            }
            stl_RTPPacket.clear();
        }
    }
    fclose(pSt_264File);
    fclose(pSt_RTPFile);
    RTPProtocol_Packet_Destory(xhSsrc);
}
void TestParse_RTP265()
{
    XNETHANDLE xhSsrc = 0;
    if (!RTPProtocol_Parse_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H265, FALSE))
    {
        printf("errrno");
        return;
    }
    LPCTSTR lpsz264File = _T("2_1.hevc");
    LPCTSTR lpszRTPFile = _T("hevc.RTP");

    FILE* pSt_264File = _tfopen(lpsz264File, _T("wb"));
    FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("rb"));
    TCHAR tszMsgBuffer[10240];
    int i = 0;

    while (1)
    {
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_RTPFile);
        if (nRet <= 0)
        {
            break;
        }
        RTPProtocol_Parse_Send(xhSsrc, tszMsgBuffer, nRet);
        while (1)
        {
            int nMsgLen = 1024000;
            TCHAR* ptszMsgBuffer = new TCHAR[nMsgLen];
            STREAMMEDIA_RTPPROTOCOL_HDR st_RTPHdr;

            memset(ptszMsgBuffer, '\0', nMsgLen);
            memset(&st_RTPHdr, '\0', sizeof(STREAMMEDIA_RTPPROTOCOL_HDR));

            if (!RTPProtocol_Parse_Recv(xhSsrc, ptszMsgBuffer, &nMsgLen, &st_RTPHdr, NULL, NULL))
            {
                delete[] ptszMsgBuffer;
                ptszMsgBuffer = NULL;
                break;
            }
            printf("%d=%d\n", i++, nMsgLen);
            fwrite(ptszMsgBuffer, 1, nMsgLen, pSt_264File);
            delete[] ptszMsgBuffer;
            ptszMsgBuffer = NULL;
        }
    }
    fclose(pSt_264File);
    fclose(pSt_RTPFile);
    RTPProtocol_Parse_Destory(xhSsrc);
}
void TestPacket_RTPAAC()
{
    XNETHANDLE xhSsrc = 0;
    if (!RTPProtocol_Packet_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_AAC, FALSE))
    {
        printf("errrno");
        return;
    }
    RTPProtocol_Packet_SetInfo(xhSsrc, 1024, 44100);
    LPCTSTR lpszAACFile = _T("test.aac");
    LPCTSTR lpszRTPFile = _T("test.RTP");

    FILE* pSt_264File = _tfopen(lpszAACFile, _T("rb"));
    FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("wb"));
    TCHAR tszMsgBuffer[10240];
    int i = 0;

    while (1)
    {
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_264File);
        if (nRet <= 0)
        {
            break;
        }
        RTPProtocol_Packet_Send(xhSsrc, tszMsgBuffer, nRet);
        while (1)
        {
            list<STREAMMEDIA_RTPPROTOCOL_PACKET> stl_RTPPacket;
            if (!RTPProtocol_Packet_Recv(xhSsrc, &stl_RTPPacket))
            {
                break;
            }
            list<STREAMMEDIA_RTPPROTOCOL_PACKET>::const_iterator stl_ListIterator = stl_RTPPacket.begin();
            for (; stl_ListIterator != stl_RTPPacket.end(); stl_ListIterator++)
            {
                printf("%d=%d\n", i, stl_ListIterator->nMsgLen);
                fwrite(stl_ListIterator->tszMsgBuffer, 1, stl_ListIterator->nMsgLen, pSt_RTPFile);
                i++;
            }
            stl_RTPPacket.clear();
        }
    }
    fclose(pSt_264File);
    fclose(pSt_RTPFile);
    RTPProtocol_Packet_Destory(xhSsrc);
}
void TestParse_RTPAAC()
{
    XNETHANDLE xhSsrc = 0;
    if (!RTPProtocol_Parse_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_AAC, FALSE))
    {
        printf("errrno");
        return;
    }
    LPCTSTR lpsz264File = _T("test_1.aac");
    LPCTSTR lpszRTPFile = _T("test.RTP");

    FILE* pSt_264File = _tfopen(lpsz264File, _T("wb"));
    FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("rb"));
    TCHAR tszMsgBuffer[10240];
    int i = 0;

    while (1)
    {
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_RTPFile);
        if (nRet <= 0)
        {
            break;
        }
        RTPProtocol_Parse_Send(xhSsrc, tszMsgBuffer, nRet);
        while (1)
        {
            int nMsgLen = 1024000;
            TCHAR* ptszMsgBuffer = new TCHAR[nMsgLen];
            STREAMMEDIA_RTPPROTOCOL_HDR st_RTPHdr;

            memset(ptszMsgBuffer, '\0', nMsgLen);
            memset(&st_RTPHdr, '\0', sizeof(STREAMMEDIA_RTPPROTOCOL_HDR));

            if (!RTPProtocol_Parse_Recv(xhSsrc, ptszMsgBuffer, &nMsgLen, &st_RTPHdr, NULL, NULL))
            {
                delete[] ptszMsgBuffer;
                ptszMsgBuffer = NULL;
                break;
            }
            printf("%d\n", i++);
            fwrite(ptszMsgBuffer, 1, nMsgLen, pSt_264File);
            delete[] ptszMsgBuffer;
            ptszMsgBuffer = NULL;
        }
    }
    fclose(pSt_264File);
    fclose(pSt_RTPFile);
    RTPProtocol_Parse_Destory(xhSsrc);
}
int main()
{
    TestPacket_RTP265();
    TestParse_RTP265();

    TestPacket_RTP264();
    TestParse_RTP264();

    TestPacket_RTPAAC();
    TestParse_RTPAAC();
    return 0;
}
